/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.lap.scd.commons.result.Result;
import com.lap.scp.admin.ao.ResourcesAO;
import com.lap.scp.admin.bean.TreeDO;
import com.lap.scp.admin.domain.ResourcesDO;
import com.lap.scp.admin.query.ResourcesQueryDO;

/**
 * <pre>
 * 资源管理
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Controller
@RequestMapping("resources")
public class ResourcesController {

	@Resource
	private ResourcesAO resourcesAO;

	@GetMapping(value = "add/{pid}")
	public ModelAndView add(@PathVariable int pid) {
		ModelAndView andView = new ModelAndView();
		List<TreeDO> treeList = resourcesAO.forAdd();

		andView.setViewName("admin/resourcesEdit");
		andView.addObject("resourcesTree", treeList);
		andView.addObject("resourcesDO", new ResourcesDO(pid));
		andView.addObject("json", JSONArray.toJSONString(treeList));

		return andView;
	}

	@PostMapping(value = "add")
	public ModelAndView add(ResourcesDO resourcesDO, BindingResult error, @RequestParam Map<String, String> map) {
		ModelAndView andView = new ModelAndView();

		Result<ResourcesDO> result = resourcesAO.insertResources(resourcesDO, error);
		if (result.isSuccess()) {
			andView.setViewName("redirect:/resources/list");
		} else {
			String json = map.get("json");
			andView.addObject("json", json);
			andView.addObject("resourcesTree", JSONArray.parseArray(json, TreeDO.class));
			andView.addObject("resourcesDO", resourcesDO);
			andView.setViewName("admin/resourcesEdit");
		}
		return andView;
	}

	@GetMapping(value = "delete/{resourcesId}")
	public ModelAndView deleteById(@PathVariable int resourcesId) {
		ModelAndView andView = new ModelAndView();

		Result<Integer> result = resourcesAO.deleteById(resourcesId);
		if (result.isSuccess()) {
			andView.setViewName("redirect:/resources/list");
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryList(ResourcesQueryDO queryDO, Integer pid) {
		ModelAndView andView = new ModelAndView();
		String json = resourcesAO.queryList(queryDO);

		andView.setViewName("admin/resourcesTree");
		andView.addObject("list", json);
		andView.addObject("resourcesQueryDO", queryDO);
		andView.addObject("pid", pid);
		return andView;
	}

	@GetMapping(value = "edit/{resourcesId}")
	public ModelAndView edit(@PathVariable int resourcesId) {
		ModelAndView andView = new ModelAndView();
		Result<ResourcesDO> result = resourcesAO.forEdit(resourcesId);
		if (result != null) {
			ResourcesDO resourcesDO = result.getData();
			andView.setViewName("admin/resourcesEdit");
			andView.addObject("resourcesDO", resourcesDO);
			andView.addObject("resourcesTree", resourcesDO.getTreeList());
			andView.addObject("json", JSONArray.toJSONString(resourcesDO.getTreeList()));
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@PostMapping(value = "edit")
	public ModelAndView edit(ResourcesDO resourcesDO, BindingResult error, @RequestParam Map<String, String> map) {
		ModelAndView andView = new ModelAndView();
		Result<Integer> result = resourcesAO.updateResources(resourcesDO, error);
		if (result.isSuccess()) {
			andView.setViewName("redirect:/resources/list");
		} else {
			andView.setViewName("admin/resourcesEdit");
			String json = map.get("json");
			andView.addObject("json", json);
			andView.addObject("resourcesTree", JSONArray.parseArray(json, TreeDO.class));
			andView.addObject("resourcesDO", resourcesDO);
		}
		return andView;
	}

	@ResponseBody
	@GetMapping(value = "listAjax")
	public List<ResourcesDO> queryListAjax(ResourcesQueryDO resourcesQueryDO) {
		return resourcesAO.queryListAjax(resourcesQueryDO);
	}

}
